<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class NilaiSiswa extends MY_Controller {

	function __construct()
    {
		// Call the Model constructor
        parent::__construct();
		if($this->session->userdata('login') === TRUE && in_array(136,$this->access)){
			$log['activity']='VIEW';
			$log['information']='Masuk ke menu : '.$this->uri->uri_string();
			$this->model_log->add($log);
		}else{
			$log['activity']='VIEW';
			$log['information']='Tidak diijinkan : '.$this->uri->uri_string();
			$this->model_log->add($log);
			redirect('sessions/login');
		}
		
		$this->config->set_item("menu_active","nilai-siswa-guru-pelajaran");
		$this->config->set_item("menu_root","penilaian");
    }
	
	public function index()
	{
		$mdl = $this->load->model('model_nilai_siswa');
		$mdl2 = $this->load->model('departemen/model_departemen');
		$mdl3 = $this->load->model('tahunAjaran/model_tahun_ajaran');
		$mdl4 = $this->load->model('kelas/model_kelas');
		$mdl5 = $this->load->model('semester/model_semester');
		$mdl7 = $this->load->model('pelajaran/model_pelajaran');
		$mdl8 = $this->load->model('kategoriRapor/model_kategori_rapor');
		$mdl9 = $this->load->model('pembagianKelas/model_pembagian_kelas');
		$mdl10 = $this->load->model('pendataanSiswa/model_pendataan_siswa');
		$mdl11 = $this->load->model('pendataanGuru/model_pendataan_guru');
		
		
		$departemen_id = 0;
		$tahun_ajaran_id = 0;
		$kelas_id = 0;
		$semester_id = 0;
		$pembagian_kelas_id = 0;
		$pelajaran_id = 0;
		$rapor_id = 0;
		$pegawai_id = 0;
		$filter = false;
		$load = false;
		
		$data['departemen']= $mdl2->get_departemen(1);
		
		if(isset($_POST['departemen'])){
			$departemen_id = $_POST['departemen'];
			$tahun_ajaran_id = isset($_POST['tahun_ajaran'])?$_POST['tahun_ajaran']:0;
			$semester_id = isset($_POST['semester'])?$_POST['semester']:0;
			$rapor_id = isset($_POST['rapor'])?$_POST['rapor']:0;
			$pelajaran_id = isset($_POST['pelajaran'])?$_POST['pelajaran']:0;
			$kelas_id = isset($_POST['kelas'])?$_POST['kelas']:0;
			$pembagian_kelas_id = isset($_POST['pembagian_kelas'])?$_POST['pembagian_kelas']:0;
			
			if(isset($_POST['filter'])){
				$filter = true;
			}
		}else if($this->session->flashdata('departemen_id')){
			$departemen_id = $this->session->flashdata('departemen_id');
			$tahun_ajaran_id = $this->session->flashdata('tahun_ajaran_id');
			$semester_id = $this->session->flashdata('semester_id');
			$rapor_id = $this->session->flashdata('rapor_id');
			$pelajaran_id = $this->session->flashdata('pelajaran_id');
			$kelas_id = $this->session->flashdata('kelas_id');
			$pembagian_kelas_id = $this->session->flashdata('pembagian_kelas_id');
			
			$filter = true;
		}else{
			foreach($data['departemen']->result_array() as $row){
				$departemen_id = $row['departemen_id'];
				break;
			}
			
		$load = true;
			
		}
		// $tingkat_id = 0;
		$data['tahun_ajaran']= $mdl3->get_tahun_ajaran($departemen_id,1);
		
		$data['semester']= $mdl5->get_semester($departemen_id,1);
		$data['rapor']=$mdl8->get_all($departemen_id,1);
		// $data['pembagian_kelas']=$mdl9->get_by();
		// $data['siswa']=$mdl10->get_siswa_id();
		
		//$data['pembagian_kelas']=$mdl9->get_pembagian_kelas($tahun_ajaran_id,$tingkat_id,$jurusan_id);
		
		if($load){
			foreach($data['rapor']->result_array() as $row){
				$rapor_id = $row['kategori_rapor_id'];
				break;
			}
		}
		
		$data['pelajaran']= $mdl7->get_all($rapor_id,1);
		
		$data['kelas']= $mdl4->get_kelas($departemen_id,1);
		
		if($load){
			foreach($data['kelas']->result_array() as $row){
				$kelas_id = $row['tingkat_id'];
				break;
			}
		}
		$data['pembagian_kelas']= $mdl9->get_all($kelas_id,1);
		
		if($filter){
			$data['nilai_siswa']= $mdl10->get_siswa_per_kelas($departemen_id,$semester_id,$tahun_ajaran_id,$kelas_id,$pembagian_kelas_id);
			// var_dump($data);
		}
		$data['guru'] = $mdl11->get_pendataan_guru($departemen_id,$rapor_id,$pelajaran_id);
		$data['departemen_id'] = $departemen_id;
		$data['tahun_ajaran_id'] = $tahun_ajaran_id;
		$data['semester_id'] = $semester_id;
		$data['rapor_id'] = $rapor_id;
		$data['pelajaran_id'] = $pelajaran_id;
		$data['kelas_id'] = $kelas_id;
		$data['pembagian_kelas_id'] = $pembagian_kelas_id;
		// $data['siswa_id'] = $siswa_id;
		
		
		$params['content'] = $this->load->view('list',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
	
	public function form($departemen_id,$tahun_ajaran_id,$semester_id,$rapor_id,$pelajaran_id,$kelas_id,$pembagian_kelas_id,$id=0)
	{
		$mdl = $this->load->model('model_nilai_siswa');
		$mdl2 = $this->load->model('departemen/model_departemen');
		$mdl3 = $this->load->model('tahunAjaran/model_tahun_ajaran');
		$mdl4 = $this->load->model('kelas/model_kelas');
		$mdl5 = $this->load->model('semester/model_semester');
		$mdl6 = $this->load->model('pelajaran/model_pelajaran');
		$mdl7 = $this->load->model('kategoriRapor/model_kategori_rapor');
		$mdl8 = $this->load->model('pegawai/model_pegawai');
		$mdl9 = $this->load->model('pembagianKelas/model_pembagian_kelas');
		$mdl10 = $this->load->model('pendataanSiswa/model_pendataan_siswa');
		
		
		
		if($id!=0){
			$id = decode($id);
		}
		
		$departemen_id = decode($departemen_id);
		$tahun_ajaran_id = decode($tahun_ajaran_id);
		$semester_id = decode($semester_id);
		$pelajaran_id = decode($pelajaran_id);
		$rapor_id = decode($rapor_id);
		$kelas_id = decode($kelas_id);
		$pembagian_kelas_id = decode($pembagian_kelas_id);
		
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			
			if($id == 0){	
				$mdl->add($data);
				
				$log['activity']='INSERT';
				$log['information']='Insert Data Nilai Detail dengan data :'.json_encode($_POST['data']);
			}else{
				$mdl->edit($id,$data);			
				$log['activity']='UPDATE';
				$log['information']="UPDATE Data Nilai Detail id $id dengan data :".json_encode($_POST['data']);
			}
			
			$this->model_log->add($log);
			
			$this->session->set_flashdata('departemen_id', $departemen_id);
			$this->session->set_flashdata('tahun_ajaran_id', $tahun_ajaran_id);
			$this->session->set_flashdata('semester_id', $semester_id);
			$this->session->set_flashdata('rapor_id', $rapor_id);
			$this->session->set_flashdata('pelajaran_id', $pelajaran_id);
			$this->session->set_flashdata('kelas_id', $kelas_id);
			$this->session->set_flashdata('pembagian_kelas_id', $pembagian_kelas_id);
			
			redirect('nilaiSiswa');
		}
		$guru_id = 0;
		$status_guru_id = 0;
		$tingkat_id = 0;
		$jurusan_id =0;
		$siswa_id = 0;
		
		$nilai_semester_id = 0;
		$data['data']= $mdl->get_by($id);
		$data['departemen'] = $mdl2->get_departemen(1);
		$data['tahun_ajaran'] = $mdl3->get_tahun_ajaran($departemen_id,1);
		$data['semester'] = $mdl5->get_semester($departemen_id,1);
		$data['rapor'] = $mdl7->get_all($departemen_id,1);
		$data['kelas'] = $mdl4->get_kelas($departemen_id,1);
		$data['pembagian_kelas'] = $mdl9->get_all($kelas_id);
		$data['pelajaran'] = $mdl6->get_all($rapor_id,1);
		$data['guru'] = $mdl8->get_pegawai();
		$data['nilai_semester'] = $mdl->get_nilai_siswa($departemen_id,$tahun_ajaran_id,$semester_id,$rapor_id,$pelajaran_id,$kelas_id,$pembagian_kelas_id);
		
		$data['departemen_id'] = $departemen_id;
		$data['tahun_ajaran_id'] = $tahun_ajaran_id;
		$data['kelas_id'] = $kelas_id;
		$data['semester_id'] = $semester_id;
		$data['pelajaran_id'] = $pelajaran_id;
		$data['rapor_id'] = $rapor_id;
		$data['pembagian_kelas_id'] = $pembagian_kelas_id;
		$data['pelajaran_id'] = $pelajaran_id;
		$data['guru_id'] = $guru_id;
		$data['nilai_semester_id'] = $nilai_semester_id;
		
		$params['content'] = $this->load->view('form',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
	
	public function delete($departemen_id,$tahun_ajaran_id,$kelas_id,$semester_id,$pelajaran_id,$rapor_id,$id=0){
		$mdl = $this->load->model('model_nilai_siswa');
		$mdl2 = $this->load->model('departemen/model_departemen');
		$mdl3 = $this->load->model('tahunAjaran/model_tahun_ajaran');
		$mdl4 = $this->load->model('kelas/model_kelas');
		$mdl5 = $this->load->model('semester/model_semester');
		$mdl6 = $this->load->model('pelajaran/model_pelajaran');
		$mdl7 = $this->load->model('kategoriRapor/model_kategori_rapor');
		
		$id = decode($id);
		$departemen_id = decode($departemen_id);
		$tahun_ajaran_id = decode($tahun_ajaran_id);
		$kelas_id = decode($kelas_id);
		$semester_id = decode($semester_id);
		$pelajaran_id = decode($pelajaran_id);
		$rapor_id = decode($rapor_id);
					
		$mdl->delete($id);
		
		$this->session->set_flashdata('departemen_id', $departemen_id);
		$this->session->set_flashdata('tahun_ajaran_id', $tahun_ajaran_id);
		$this->session->set_flashdata('kelas_id', $kelas_id);
		$this->session->set_flashdata('semester_id', $semester_id);
		$this->session->set_flashdata('pelajaran_id', $pelajaran_id);
		$this->session->set_flashdata('rapor_id', $rapor_id);
			
		redirect('nilaiSiswa');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */